我试图用Vue 3和typescript初始化一个项目,但在将vuex添加到项目后,它无法编译。我做了什么:1.首先,我使用create-vue创建了这个项目,使用了recommend选项:Creating Vue Project1.已安装vuex:
npm install vuex@next --save1.按照Vuex Docs中的步骤: typescript 支持所以,在我的简单存储文件中,我有:
import { type InjectionKey } from 'vue'import { createStore, Store } from 'vuex'// define your typings for the store stateexport interface State { count: number}// define injection keyexport const key: InjectionKey = Symbol()export const store = createStore({ state: {count: 0 }})但是如果我运行npm run build,结果如下:
src/stores/index.ts:2:36 - error TS7016: Could not find a declaration file for module 'vuex'. '/home/user/projects/vue_projects/init-test/node_modules/vuex/dist/vuex.mjs' implicitly has an 'any' type. There are types at '/home/user/projects/vue_projects/init-test/node_modules/vuex/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'vuex' library may need to update its package.json or typings.2 import { createStore, Store } from 'vuex'它似乎已经找到了vuex类型,但无论如何都无法导入。我该怎么解决这个问题?我已经添加了一个vuex.d.ts,遵循vuex文档。
import { Store } from 'vuex'declare module '@vue/runtime-core' { // declare your own store states interface State {count: number } // provide typings for `this.$store` interface ComponentCustomProperties {$store: Store }}